package com.offcn.spark.p3

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Auther: BigData-LGW
 * @ClassName: GroupBy
 * @Date: 2020/12/7 19:05
 * @功能描述: $FunctionDescription
 * @Version:1.0
 */
object GroupBy {

    def main(args: Array[String]): Unit = {
        val conf = new SparkConf()
            .setMaster("local[*]")
            .setAppName("GroupBy")
        val sc = new SparkContext(conf)
        groupBy(sc)
        sc.stop()
    }
    def groupBy(sc: SparkContext) = {
        case class Student(id:Int,name:String,province:String)
        val stuRDD = sc.parallelize(List(
            Student(1, "白普州", "安徽"),
            Student(2, "伍齐城", "山东"),
            Student(3, "曹佳", "甘肃"),
            Student(4, "姚远", "甘肃"),
            Student(5, "匿名大哥", "黑吉辽"),
            Student(10086, "欧阳龙生", "黑吉辽")
        ))
        val province2Infos:RDD[(String,Iterable[Student])] = stuRDD.map(stu =>
            (stu.province,stu)).groupByKey()
        province2Infos.foreach{
            case (province,stus) => {
                println(s"省份：${province}, 学生信息：${stus.mkString(", ")}, 人数：${stus.size}")
            }
        }
    }
}
